package com.kanas.webak.config.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.config.domain.Properties;
import com.kanas.webak.config.domain.PropertiesGroup;
import com.kanas.webak.config.service.PropertiesService;

@Controller
@RequestMapping("app/config/properties")
public class PropertiesController extends ControllerSupport {

	private PropertiesService propertiesService;

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView properties(){
		ModelAndView mv=new ModelAndView();
		mv.addObject("groups", this.propertiesService.findGroups());
		mv.setViewName("modules/sysadmin/properties");
		return mv;
	}

	@RequestMapping(value="/group/new", method=RequestMethod.GET)
	public ModelAndView newGroup(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/properties_group_new");
		return mv;
	}

	@RequestMapping(value="/group/new", method=RequestMethod.POST)
	public @ResponseBody ResponseMessage addNewGroup(final PropertiesGroup group) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return propertiesService.createGroup(group);
			}
		}, logger);
		return response;
	}

	@RequestMapping(value="/group/{groupcode}/properties",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAll(@PathVariable("groupcode") String groupCode){
		Map<String,Object> response=new HashMap<String,Object>();
		response.put("identifier", "key");
		response.put("label", "key");
		response.put("items", this.getPropertiesService().findByGroup(groupCode));
		return response;
	}
	
	@RequestMapping(value="/new", method=RequestMethod.GET)
	public ModelAndView newProperties(@RequestParam(value="groupcode",required=false)String groupCode){
		ModelAndView mv=new ModelAndView();
		mv.addObject("groups", this.propertiesService.findGroups());
		mv.addObject("currentGroup",groupCode);
		mv.setViewName("modules/sysadmin/properties_new");
		return mv;
	}
	
	@RequestMapping(value="/new", method=RequestMethod.POST)
	public @ResponseBody ResponseMessage create(final Properties p, @RequestParam("groupcode") final String groupCode) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return propertiesService.create(p,groupCode);
			}
		}, logger);
		return response;
	}

	@RequestMapping(value="/{propertiesKey}", method=RequestMethod.PUT)
	public @ResponseBody ResponseMessage update(@PathVariable("propertiesKey") final String key, 
			@RequestParam(value="value",required=false) final String value, HttpServletRequest request) {
		
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				Properties p = new Properties();
				p.setKey(key);
				p.setValue(value);
				return propertiesService.updateValue(p);
			}
		}, logger);
		return response;
	}
	
	public PropertiesService getPropertiesService() {
		return propertiesService;
	}

	@Resource(name="com.kanas.webak.config.service.propertiesservice")
	public void setPropertiesService(PropertiesService propertiesService) {
		this.propertiesService = propertiesService;
	}
}
